/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.peopleinformation.appraisalindicatorsdata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.peopleinformation.appraisalindicatorsdata.entity.PeopleAppraisalInformation;
import com.jeeplus.modules.peopleinformation.appraisalindicatorsdata.mapper.PeopleAppraisalInformationMapper;
import com.jeeplus.modules.peopleinformation.appraisalindicatorsdata.entity.PeopleAppraisalInformationAttach;
import com.jeeplus.modules.peopleinformation.appraisalindicatorsdata.mapper.PeopleAppraisalInformationAttachMapper;

/**
 * 考评指标信息Service
 * @author 刘明星
 * @version 2021-04-19
 */
@Service
@Transactional(readOnly = true)
public class PeopleAppraisalInformationService extends CrudService<PeopleAppraisalInformationMapper, PeopleAppraisalInformation> {

	@Autowired
	private PeopleAppraisalInformationAttachMapper peopleAppraisalInformationAttachMapper;

	public PeopleAppraisalInformation get(String id) {
		PeopleAppraisalInformation peopleAppraisalInformation = super.get(id);
		peopleAppraisalInformation.setPeopleAppraisalInformationAttachList(peopleAppraisalInformationAttachMapper.findList(new PeopleAppraisalInformationAttach(peopleAppraisalInformation)));
		return peopleAppraisalInformation;
	}

	public List<PeopleAppraisalInformation> findList(PeopleAppraisalInformation peopleAppraisalInformation) {
		return super.findList(peopleAppraisalInformation);
	}

	public Page<PeopleAppraisalInformation> findPage(Page<PeopleAppraisalInformation> page, PeopleAppraisalInformation peopleAppraisalInformation) {
		return super.findPage(page, peopleAppraisalInformation);
	}

	@Transactional(readOnly = false)
	public void save(PeopleAppraisalInformation peopleAppraisalInformation) {
		super.save(peopleAppraisalInformation);
		for (PeopleAppraisalInformationAttach peopleAppraisalInformationAttach : peopleAppraisalInformation.getPeopleAppraisalInformationAttachList()){
			if (peopleAppraisalInformationAttach.getId() == null){
				continue;
			}
			if (PeopleAppraisalInformationAttach.DEL_FLAG_NORMAL.equals(peopleAppraisalInformationAttach.getDelFlag())){
				if (StringUtils.isBlank(peopleAppraisalInformationAttach.getId())){
					peopleAppraisalInformationAttach.setAppraisal(peopleAppraisalInformation);
					peopleAppraisalInformationAttach.preInsert();
					peopleAppraisalInformationAttachMapper.insert(peopleAppraisalInformationAttach);
				}else{
					peopleAppraisalInformationAttach.preUpdate();
					peopleAppraisalInformationAttachMapper.update(peopleAppraisalInformationAttach);
				}
			}else{
				peopleAppraisalInformationAttachMapper.delete(peopleAppraisalInformationAttach);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(PeopleAppraisalInformation peopleAppraisalInformation) {
		super.delete(peopleAppraisalInformation);
		peopleAppraisalInformationAttachMapper.delete(new PeopleAppraisalInformationAttach(peopleAppraisalInformation));
	}

}